package com.kuang.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.api.R;
import com.kuang.mapper.WuliuMapper;
import com.kuang.pojo.Wuliu;
import com.kuang.result.CommonResult;
import com.kuang.result.ResultCodeEnum;
import com.kuang.service.WuliuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.lang.String;

import java.util.List;

/**
 * Created by Intellij IDEA.
 * User:  xsw
 * Date:  2020/8/1
 */
@Controller
public class WuliuController {

    @Autowired
    private WuliuMapper wuliuMapper;

    @RequestMapping("/aa")
    public String aa() {
        return "aa";
    }

    @RequestMapping("/ss")
    public String ss() {
        return "admin/adminIndex";
    }

    @RequestMapping("/insert")
    public String insert() {
        return "/admin/insert";
    }

    @ApiOperation(value = "删除信息 ")
    @RequestMapping(value = "/delete", method = {RequestMethod.POST})
    @ResponseBody
    public CommonResult delete(Wuliu wuliu) {
        return CommonResult.success(wuliuMapper.deleteById(wuliu.getWid()));
    }

    /**
     * 查询没有对数据进行改变，所以是使用GET请求
     *
     * @return wuliu表实体数据
     */
    @ApiOperation(value = "查询列表")
    @GetMapping("/hello2")
    @ResponseBody
    public CommonResult<List<Wuliu>> jsonR() {
        return CommonResult.success(wuliuMapper.selectList(null));
    }

    @ApiOperation(value = "根据id查询信息")
    @RequestMapping(value = "/selectById/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult selectById(@PathVariable("id") Integer id) {
        Wuliu wuliu = wuliuMapper.selectById(id);
        return CommonResult.success(wuliu);
    }

    @ApiOperation(value = "获取id进行数据的更新")
    @RequestMapping(value = "/admin/update", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult updateInit(Wuliu wuliu) {
        // 1、获取id后 将整条数据带入新的页面进行操作
        return CommonResult.success(wuliuMapper.selectById(wuliu.getWid()));
    }

    @ApiOperation(value = "获取id进行数据的更新")
    @RequestMapping(value = "/admin/update", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updateById(Wuliu wuliu) {
        // 1、获取id后 将整条数据带入新的页面进行操作
        return CommonResult.success(wuliuMapper.updateById(wuliu));
    }

    @ApiOperation(value = "添加数据")
    @RequestMapping(value = "/admin/add", method = {RequestMethod.POST})
    @ResponseBody
    public CommonResult insert(Wuliu wuliu) {
        return CommonResult.success(wuliuMapper.insert(wuliu));
    }

    @ApiOperation(value = "根据name查询数据")
    @RequestMapping(value = "/admin/selectByName", method = {RequestMethod.GET})
    @ResponseBody
    public CommonResult<List<Wuliu>> selectByName(@RequestParam("name") String name) {
        List<Wuliu> wuliuByName = wuliuMapper.findWuliuByName("%" + name + "%");
        wuliuByName.forEach(System.out::println);
        return CommonResult.success(wuliuByName);
    }
}
